package com.cskaoyan.market.service.impl;
import java.util.HashMap;
import java.util.List;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.service.WxHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @ClassName WxHomeService
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/4/1 9:59
 * @Version V1.0
 **/
@Service
public class WxHomeServiceImpl implements WxHomeService {

    private MarketCategory.Column[] channels = {MarketCategory.Column.id, MarketCategory.Column.name,MarketCategory.Column.iconUrl};

    @Autowired
    MarketAdMapper adMapper;

    @Autowired
    MarketCategoryMapper categoryMapper;

    @Autowired
    MarketCouponMapper couponMapper;

    /**
     * 分别需要从ad表、category表、coupon表查询数据，最终返回结果
     * @return
     */
    @Override
    public Map<String, Object> index() {
        List<MarketAd> marketAdList = queryBanner();
        List<MarketCategory> categoryList = queryChannel();
        List<MarketCoupon> couponList = queryCoupon();
        Map<String ,Object> result = new HashMap<>();
        result.put("banner", marketAdList);
        result.put("channel", categoryList);
        result.put("couponList", couponList);
        return result;
    }

    private List<MarketCoupon> queryCoupon() {
        MarketCouponExample couponExample = new MarketCouponExample();
        couponExample.or().andTypeEqualTo((short)0).andDeletedEqualTo(false);
        return couponMapper.selectByExample(couponExample);
    }

    private List<MarketCategory> queryChannel() {
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        categoryExample.or().andPidEqualTo(0).andDeletedEqualTo(false);
        return categoryMapper.selectByExampleSelective(categoryExample, channels);
    }

    private List<MarketAd> queryBanner() {
        MarketAdExample adExample = new MarketAdExample();
        adExample.or().andPositionEqualTo((byte)1).andDeletedEqualTo(false);
        return adMapper.selectByExample(adExample);
    }
}
